home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / Kibitz / IdleTasks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-12  |  5.4 KB  |  210 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        idletasks.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __UTILITIES__
  21. #include <Utilities.h>
  22. #endif
  23.  
  24.  
  25.  
  26. /*****************************************************************************/
  27.  
  28.  
  29.  
  30. #pragma segment Main
  31. void    DoIdleTasks(Boolean allowComputerMoves)
  32. {
  33.     WindowPtr        window, compMoveWindow;
  34.     unsigned long    compMoveTick;
  35.     FileRecHndl        game, compMoveGame;
  36.     FileRecPtr        frPtr;
  37.     short            twoPlayer, moveColor, myColor, update, syncClocks, i;
  38.     Boolean            compMovesWhite, compMovesBlack, sendSyncGame;
  39.     OSErr            err;
  40.  
  41.     static unsigned long    clockSyncTick;
  42.     static Boolean            startingUp = true;
  43.     static long                waitSome;
  44.  
  45.     if (startingUp) {
  46.         if (FrontWindow()) {
  47.             startingUp = false;
  48.             return;
  49.         }
  50.         if (!waitSome) waitSome = TickCount() + 30;
  51.         else {
  52.             if (waitSome < TickCount()) {
  53.                 startingUp = false;
  54.                 err = AppNewDocument(&game, ksOrigName);
  55.                 if (!err) {
  56.                     (*game)->doc.compMovesBlack = true;
  57.                     err = AppNewWindow(game, nil, (WindowPtr)-1);
  58.                     if (err) {
  59.                         AppDisposeDocument(game);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         return;
  65.     }
  66.  
  67.     DynamicBalloonHelp();
  68.  
  69.     compMoveTick = -1;
  70.  
  71.     syncClocks = (clockSyncTick + 1800 < TickCount());
  72.     if (syncClocks)
  73.         clockSyncTick = TickCount();
  74.             /* Syncronize clocks every 30 seconds. */
  75.  
  76.  
  77.     for (window = FrontWindow();
  78.          window;
  79.          window = (WindowPtr)(((WindowPeek)window)->nextWindow)
  80.     ) {
  81.         if (IsAppWindow(window)) {
  82.  
  83.             DoSetCursor(nil);
  84.  
  85.             game = (FileRecHndl)GetWRefCon(window);
  86.             twoPlayer = (*game)->doc.twoPlayer;
  87.  
  88.             update = UpdateTime(game, true);
  89.             if (update) DrawTime(game);
  90.  
  91.             if (update == 2) {
  92.                 if (twoPlayer) SendGame(game, kIsMove, nil);
  93.                     /* Send it as a move, since we want the alert to
  94.                     ** show up for the opponent. */
  95.                 AlertIfGameOver(game);
  96.                 return;
  97.             }
  98.  
  99.             if ((*game)->doc.resync >= kResync) {
  100.                 sendSyncGame = true;
  101.                     /* We may need to sync up.  Assume we will. */
  102.  
  103.                 if ((*game)->doc.configColorChange) {
  104.                     if ((*game)->doc.myColor != (*game)->doc.configColor) {
  105.                         (*game)->doc.myColor = (*game)->doc.configColor;
  106.                         (*game)->doc.invertBoard ^= 1;
  107.                         SetPort(window);
  108.                         ImageDocument(game, true);
  109.                     }
  110.                     (*game)->doc.configColorChange = false;
  111.                     AdjustGameSlider(game);
  112.                     sendSyncGame = false;
  113.                 }
  114.  
  115.                 if ((*game)->doc.configTimeChange) {
  116.                     for (i = 0; i < 2; ++i)
  117.                         (*game)->doc.timeLeft[i] =
  118.                             (*game)->doc.displayTime[i] =
  119.                                 (*game)->doc.configTime[i];
  120.                     (*game)->doc.configTimeChange = false;
  121.                     UpdateTime(game, false);
  122.                     DrawTime(game);
  123.                     sendSyncGame = false;
  124.                 }
  125.  
  126.                 if (sendSyncGame) {
  127.                     if ((*game)->doc.gotUpdateTick + 120 < TickCount()) {
  128.                         /* Wait for 2 secs since last game update before syncing.
  129.                         ** This is so that we don't send a sync while the opponent
  130.                         ** is still clicking on the arrow.  Without this delay, the
  131.                         ** scrollbar of the opponent will jump around after he is
  132.                         ** done scrolling.  It would eventually end up correct,
  133.                         ** but it looks bad.  2 secs is enough time (generally) for
  134.                         ** the opponent to receive the sync from the last click, so
  135.                         ** the scroll won't jump around.  Also, 2 secs is probably
  136.                         ** more time than the user would take between clicks on
  137.                         ** the arrow. */
  138.  
  139.                         if ((*game)->doc.creator)        /* Only the creator can sync. */
  140.                             if (twoPlayer) SendGame(game, kHandResync, nil);
  141.                                 /* Make sure the boards are in sync. */
  142.  
  143.                         (*game)->doc.resync = kIsMove;
  144.                             /* Back to life as usual. */
  145.  
  146.                         DoSetCursor(nil);
  147.                             /* Re-calc the cursor. */
  148.                     }
  149.                 }
  150.             }
  151.  
  152.             frPtr          = *game;
  153.             compMovesWhite = frPtr->doc.compMovesWhite;
  154.             compMovesBlack = frPtr->doc.compMovesBlack;
  155.             myColor        = frPtr->doc.myColor;
  156.             moveColor      = WhosMove(game);
  157.  
  158.             if (frPtr->doc.arrangeBoard)
  159.                 compMovesWhite = compMovesBlack = 0;
  160.  
  161.             if (twoPlayer) {
  162.                 if (myColor == moveColor) {
  163.                     if (syncClocks)
  164.                         SendMssg(game, kTimeMssg);
  165.                             /* Sync up the clocks every 30 seconds.  Only do
  166.                             ** this if it is our move, since the player who
  167.                             ** owns the move also owns the clock. */
  168.                 }
  169.                 else
  170.                     compMovesWhite = compMovesBlack = false;
  171.             }
  172.             if (
  173.                 ((compMovesWhite) && (moveColor == WHITE)) ||
  174.                 ((compMovesBlack) && (moveColor == BLACK))
  175.             ) {
  176.                 if (compMoveTick > frPtr->doc.compMoveTick) {
  177.                     if (GameStatus(game) == kGameContinues) {
  178.                         compMoveTick   = frPtr->doc.compMoveTick;
  179.                         compMoveWindow = window;
  180.                         compMoveGame   = game;
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.     }
  186.  
  187.     if (allowComputerMoves) {
  188.         if (compMoveTick != -1) {
  189.             if ((*compMoveGame)->doc.resync == kIsMove) {
  190.                 (*compMoveGame)->doc.compMoveTick = TickCount();
  191.                 if (ComputerMove(compMoveGame)) {
  192.                     SetPort(compMoveWindow);
  193.                     ImageDocument(compMoveGame, true);
  194.                     AdjustGameSlider(compMoveGame);
  195.                     twoPlayer = (*compMoveGame)->doc.twoPlayer;
  196.                     DrawButtonTitle(compMoveGame, twoPlayer);
  197.                     UpdateGameStatus(compMoveGame);
  198.                     if (twoPlayer) SendGame(compMoveGame, kIsMove, nil);
  199.                     SayTheMove(compMoveGame);
  200.                     AlertIfGameOver(compMoveGame);
  201.                     DoSetCursor(nil);
  202.                 }
  203.             }
  204.         }
  205.     }
  206. }
  207.  
  208.  
  209.  
  210.